﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using craigsPack;
using System.Net.Mail;
using System.Net;

namespace Example1
{
    class Program
    {
        static void Main(string[] args)
        {
            CraigsListProvider provider = new CraigsListProvider();
            provider.SearchComplete += new EventHandler<SearchResultsEventArgs>(provider_SearchComplete);
            provider.FoundResults += new EventHandler<SearchResultsEventArgs>(provider_FoundResults);

            CraigsListSearchSettings searchSettings = new CraigsListSearchSettings();
            searchSettings.Category = CraigsListCategory.Sale;
            searchSettings.MaxPrice = 8000;
            Console.Write("Enter Keyword : ");

            searchSettings.Keywords.Add(Console.ReadLine());
            searchSettings.Cities.Add(CityArea.Ventura);
            searchSettings.Cities.Add(CityArea.La);
            searchSettings.LatestDate = new WeakDate("3", "1");

            Console.WriteLine("Searching...");
            provider.Search(searchSettings);
            Console.ReadLine();
        }

        static void provider_FoundResults(object sender, SearchResultsEventArgs e)
        {
            var items = (KeyValuePair<string, CraigsListItemInfo[]>)e.Results;
        }

        static void provider_SearchComplete(object sender, SearchResultsEventArgs e)
        {
            Console.WriteLine("Found " + ((CraigsListItemInfo[])e.Results).Length.ToString());
            Console.WriteLine();
            Console.WriteLine("Results For All Keywords:" );
            CraigsListItemInfo[] items = (e.Results as CraigsListItemInfo[]);

            for (int i = 0; i < items.Length; i++)
            {
                Console.WriteLine(string.Format((i + 1).ToString () + ": {0} {1}", new object[] { (items[i].DatePosted.Month.ToString() + ' ' + items[i].DatePosted.Day.ToString() + '-'), items[i].Link.ToString() }));
            }
        }
    }
}
